<template>
  <div class="device-management">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="设备名称">
          <el-input v-model="searchForm.deviceName" placeholder="请输入设备名称" clearable />
        </el-form-item>
        <el-form-item label="所在工程">
          <el-input v-model="searchForm.factory" placeholder="请输入所在工程" clearable />
        </el-form-item>
        <el-form-item label="上线时间">
          <el-date-picker
            v-model="searchForm.onlineTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="当前状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="在线" value="online" />
            <el-option label="离线" value="offline" />
            <el-option label="故障" value="fault" />
            <el-option label="停用" value="disabled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <div class="table-area">
      <el-table :data="deviceList" style="width: 100%" border>
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="deviceName" label="设备名称" min-width="120" />
        <el-table-column prop="algorithmType" label="算法类型" min-width="120" />
        <el-table-column prop="deviceCode" label="设备编码" min-width="120" />
        <el-table-column prop="factory" label="所在工程" min-width="120" />
        <el-table-column prop="siteInfo" label="站点信息" min-width="120" />
        <el-table-column prop="status" label="当前状态" width="100" align="center">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="onlineTime" label="上线时间" width="180" />
        <el-table-column label="操作" width="260" align="center" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="handleDetail(scope.row)">查看详情</el-button>
            <el-button 
              size="small" 
              :type="scope.row.status === 'disabled' ? 'success' : 'warning'"
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.status === 'disabled' ? '启用' : '停用' }}
            </el-button>
            <el-button 
              size="small" 
              type="danger" 
              @click="handleReportFault(scope.row)"
              :disabled="scope.row.status === 'fault'"
            >
              故障上报
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-area">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 设备详情对话框 -->
    <el-dialog
      v-model="detailDialogVisible"
      title="设备详情"
      width="600px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="设备名称">{{ currentDevice.deviceName }}</el-descriptions-item>
        <el-descriptions-item label="设备编码">{{ currentDevice.deviceCode }}</el-descriptions-item>
        <el-descriptions-item label="算法类型">{{ currentDevice.algorithmType }}</el-descriptions-item>
        <el-descriptions-item label="所在工程">{{ currentDevice.factory }}</el-descriptions-item>
        <el-descriptions-item label="站点信息">{{ currentDevice.siteInfo }}</el-descriptions-item>
        <el-descriptions-item label="当前状态">
          <el-tag :type="getStatusType(currentDevice.status)">
            {{ getStatusText(currentDevice.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="上线时间" :span="2">{{ currentDevice.onlineTime }}</el-descriptions-item>
        <el-descriptions-item label="设备描述" :span="2">{{ currentDevice.description }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 故障上报对话框 -->
    <el-dialog
      v-model="faultDialogVisible"
      title="故障上报"
      width="500px"
    >
      <el-form :model="faultForm" label-width="100px">
        <el-form-item label="故障类型">
          <el-select v-model="faultForm.type" placeholder="请选择故障类型">
            <el-option label="硬件故障" value="hardware" />
            <el-option label="软件故障" value="software" />
            <el-option label="网络故障" value="network" />
            <el-option label="其他故障" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="故障描述">
          <el-input
            v-model="faultForm.description"
            type="textarea"
            rows="4"
            placeholder="请输入故障描述"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="faultDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitFault">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  deviceName: '',
  factory: '',
  onlineTime: [],
  status: ''
})

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(10) // 十条测试数据

// 表格数据
const deviceList = ref([
  {
    deviceName: '摄像头A1',
    algorithmType: '人员检测',
    deviceCode: 'CAM001',
    factory: '矿山一号',
    siteInfo: '监测点A',
    status: 'online',
    onlineTime: '2024-03-15 08:00:00',
    description: '用于检测人员安全帽佩戴情况的摄像头'
  },
  {
    deviceName: '传感器B2',
    algorithmType: '环境监测',
    deviceCode: 'SEN002',
    factory: '矿山二号',
    siteInfo: '监测点B',
    status: 'fault',
    onlineTime: '2024-03-14 09:30:00',
    description: '用于监测环境温度、湿度的传感器'
  },
  {
    deviceName: '摄像头C3',
    algorithmType: '车辆识别',
    deviceCode: 'CAM003',
    factory: '矿山一号',
    siteInfo: '监测点C',
    status: 'online',
    onlineTime: '2024-03-15 10:15:00',
    description: '用于监测车辆进出的摄像头'
  },
  {
    deviceName: '气体检测器D4',
    algorithmType: '有害气体检测',
    deviceCode: 'GAS004',
    factory: '矿山三号',
    siteInfo: '监测点D',
    status: 'offline',
    onlineTime: '2024-03-13 14:20:00',
    description: '用于检测有害气体浓度的传感器'
  },
  {
    deviceName: '振动传感器E5',
    algorithmType: '地质监测',
    deviceCode: 'VIB005',
    factory: '矿山二号',
    siteInfo: '监测点E',
    status: 'online',
    onlineTime: '2024-03-15 07:45:00',
    description: '用于监测地质振动的传感器'
  },
  {
    deviceName: '摄像头F6',
    algorithmType: '设备监控',
    deviceCode: 'CAM006',
    factory: '矿山一号',
    siteInfo: '监测点F',
    status: 'disabled',
    onlineTime: '2024-03-12 16:30:00',
    description: '用于监控设备运行状态的摄像头'
  },
  {
    deviceName: '噪声检测器G7',
    algorithmType: '噪声监测',
    deviceCode: 'NOI007',
    factory: '矿山三号',
    siteInfo: '监测点G',
    status: 'online',
    onlineTime: '2024-03-15 09:10:00',
    description: '用于监测环境噪声的传感器'
  },
  {
    deviceName: '粉尘检测器H8',
    algorithmType: '粉尘监测',
    deviceCode: 'DUS008',
    factory: '矿山二号',
    siteInfo: '监测点H',
    status: 'fault',
    onlineTime: '2024-03-14 11:25:00',
    description: '用于监测空气中粉尘浓度的传感器'
  },
  {
    deviceName: '摄像头I9',
    algorithmType: '人员计数',
    deviceCode: 'CAM009',
    factory: '矿山一号',
    siteInfo: '监测点I',
    status: 'online',
    onlineTime: '2024-03-15 08:30:00',
    description: '用于统计区域内人员数量的摄像头'
  },
  {
    deviceName: '水质检测器J10',
    algorithmType: '水质监测',
    deviceCode: 'WAT010',
    factory: '矿山三号',
    siteInfo: '监测点J',
    status: 'online',
    onlineTime: '2024-03-15 07:00:00',
    description: '用于监测水质参数的传感器'
  }
])

// 当前选中的设备
const currentDevice = ref({})

// 对话框显示控制
const detailDialogVisible = ref(false)
const faultDialogVisible = ref(false)

// 故障表单
const faultForm = reactive({
  type: '',
  description: ''
})

// 状态相关方法
const getStatusType = (status) => {
  const statusMap = {
    online: 'success',
    offline: 'info',
    fault: 'danger',
    disabled: 'warning'
  }
  return statusMap[status]
}

const getStatusText = (status) => {
  const statusMap = {
    online: '在线',
    offline: '离线',
    fault: '故障',
    disabled: '停用'
  }
  return statusMap[status]
}

// 搜索方法
const handleSearch = () => {
  console.log('搜索条件：', searchForm)
  // TODO: 调用接口进行搜索
  currentPage.value = 1
}

// 重置搜索
const resetSearch = () => {
  searchForm.deviceName = ''
  searchForm.factory = ''
  searchForm.onlineTime = []
  searchForm.status = ''
  handleSearch()
}

// 查看详情
const handleDetail = (row) => {
  currentDevice.value = row
  detailDialogVisible.value = true
}

// 启用/停用设备
const handleToggleStatus = (row) => {
  const action = row.status === 'disabled' ? '启用' : '停用'
  ElMessageBox.confirm(
    `确定要${action}该设备吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 调用接口进行状态更新
    row.status = row.status === 'disabled' ? 'online' : 'disabled'
    ElMessage.success(`${action}成功`)
  }).catch(() => {})
}

// 故障上报
const handleReportFault = (row) => {
  currentDevice.value = row
  faultForm.type = ''
  faultForm.description = ''
  faultDialogVisible.value = true
}

// 提交故障报告
const submitFault = () => {
  if (!faultForm.type || !faultForm.description) {
    ElMessage.warning('请填写完整的故障信息')
    return
  }
  // TODO: 调用接口提交故障报告
  currentDevice.value.status = 'fault'
  ElMessage.success('故障上报成功')
  faultDialogVisible.value = false
}

// 分页方法
const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}
</script>

<style scoped>
.device-management {
  height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

.search-area {
  padding: 16px;
  background-color: #fff;
  border-radius: 4px;
  margin-bottom: 16px;
}

.table-area {
  flex: 1;
  background-color: #fff;
  border-radius: 4px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.table-area :deep(.el-table) {
  flex: 1;
}

.table-area :deep(.el-table__body-wrapper) {
  overflow-y: auto !important;
}

.pagination-area {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 确保表格内容不会被操作列遮挡 */
.table-area :deep(.el-table__fixed-right) {
  height: 100% !important;
}
</style>